<?

function   doPayment($mid,$transid,$minimum_amount)
  {



     $sql_trns ="select * from partners_transaction,partners_joinpgm where transaction_id=$transid and joinpgm_id=transaction_joinpgmid";
     $ret_trns =mysql_query($sql_trns);

     //checking for each records
     if(mysql_num_rows($ret_trns)>0)
     {
             $row_trns		=mysql_fetch_object($ret_trns);
             $amount        =$row_trns->transaction_amttobepaid ;
             $subsale       =$row_trns->transaction_subsale;
             $parentid      =$row_trns->transaction_parentid;
             $admin_amount  =$row_trns->transaction_admin_amount;
             $flag			=$row_trns->transaction_flag;
             $aid			=$row_trns->joinpgm_affiliateid;
     }


	//----------------------------------------  get merchant account   --------------------------------------//

	                    $sql        ="SELECT *  FROM `merchant_pay`  WHERE pay_merchantid='$mid' ";
	                    $res        =mysql_query($sql);

                        $num	  = mysql_num_rows($res);
                        $cut_off  =	$minimum_amount;
                        if($num>0)
	                    {
	                        $row1   			=   mysql_fetch_object($res);
                            $merchant_balance   = $row1->pay_amount;

                           if(($merchant_balance-($amount+$admin_amount) )<=$cut_off)
                             {
                         			return(0);
                             }
                             else
                            {


						//---------------------------------------- Adding money to affiliate account  --------------------------//
                                $merchant_balance = $merchant_balance-($amount+$admin_amount);
                                $sql1       ="SELECT *  FROM `affiliate_pay` where pay_affiliateid=$aid ";
                                $res1       =mysql_query($sql1);
                                echo mysql_error();

                                 $num=mysql_num_rows($res1);
                                 if($num>0)
                                 {
                                    $row1	=	mysql_fetch_object($res1);
                                  	$curamount	= $row1->pay_amount;
                                    $curamount	= $curamount+$amount;
		                            $sql2 ="update affiliate_pay set pay_amount='$curamount' where pay_affiliateid=$aid";
        		                    $ret2 =mysql_query($sql2);
                                 }
                                 else
                                 {
		                            $sql2 ="INSERT INTO  affiliate_pay set pay_affiliateid=$aid , pay_amount='$amount";
        		                    $ret2 =mysql_query($sql2);
                                 }

   			   	 //----------------------------------- affiliate account editing Ends here--------------------------//

    			//---------------------------------- Subtract Money From Merchant Account ---------------------------//
                                   $sql        ="UPDATE  `merchant_pay` SET  pay_amount=$merchant_balance  WHERE pay_merchantid='$mid' ";
                                   $res        =mysql_query($sql);
                                   $_SESSION['MERCHANTBALANCE']= $merchant_balance;
    			//--------------------------------- END OF  Subtract Money From Merchant Account ---------------------------//

    			//--------------------------------- Adding To Admins Account-----------------------------------------//

                                 $sql1		="SELECT *  FROM `admin_pay`  ";
                                 $res1		=mysql_query($sql1);
                                 echo mysql_error();
                                 $num=mysql_num_rows($res1);
                                 if($num>0)
                                 {
                                    $row1	=	mysql_fetch_object($res1);
                                  	$admin_curamount	= $row1->pay_amount;
                                    $admin_curamount	= ($admin_curamount+$admin_amount)-$subsale;
                                 }

                                $sql		="UPDATE `admin_pay` SET `pay_amount` = '$admin_curamount'";
                                $res		=mysql_query($sql);
                               if($flag==1)
                               {
                                $sql1       ="SELECT *  FROM `affiliate_pay` where pay_affiliateid=$parentid ";
                                $res1       =mysql_query($sql1);
                                echo mysql_error();

                                 $num=mysql_num_rows($res1);
                                 if($num>0)
                                 {
                                    $row1	=	mysql_fetch_object($res1);
                                  	$curamount	= $row1->pay_amount;
                                    $curamount	= $curamount+$subsale;
		                            $sql2 ="update affiliate_pay set pay_amount='$curamount' where pay_affiliateid=$parentid";
        		                    $ret2 =mysql_query($sql2);
                                 }
                               }
    			 //--------------------------------- END OF Adding to admin's account -------------------------------//
                             }
                         }
      return(1);}
?>